<template>
  <page-container title="修改密码" :showBack="true">
    <template v-slot:nav-left>
      <view class="nav-left" @click="goBack">
        <uni-icons type="left" size="20" color="#d4af37"></uni-icons>
      </view>
    </template>
    <view class="change-pwd-container">
      <view class="form-container">
        <!-- 手机号输入框 -->
        <view class="input-group">
          <input class="input-field" type="number" placeholder="请输入手机号" v-model="phone" maxlength="11" readonly
            disabled />
        </view>

        <!-- 验证码输入框 -->
        <view class="input-group">
          <input class="input-field verification-input" type="number" placeholder="请输入您的验证码" v-model="verificationCode"
            maxlength="6" />
          <button class="verification-btn" :disabled="countdown > 0" @click="getVerificationCode">
            {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
          </button>
        </view>

        <!-- 密码输入框 -->
        <view class="input-group">
          <input class="input-field password-input" v-model="password" placeholder="请输入密码" :password="!showPassword"/>
          <view class="eye-icon" @click="togglePasswordVisibility">
            <uni-icons :type="showPassword ? 'eye' : 'eye-slash'" size="20" color="#999999"></uni-icons>
          </view>
        </view>

        <!-- 确认密码输入框 -->
        <view class="input-group">
          <input class="input-field password-input" v-model="confirmPassword" placeholder="请再次输入密码" :password="!showConfirmPassword"/>
          <view class="eye-icon" @click="toggleConfirmPasswordVisibility">
            <uni-icons :type="showConfirmPassword ? 'eye' : 'eye-slash'" size="20" color="#999999"></uni-icons>
          </view>
        </view>

        <!-- 确认按钮 -->
        <button class="confirm-btn" @click="handleSubmit">
          确认
        </button>
      </view>
    </view>
  </page-container>
</template>

<script>
export default {
  name: 'ChangePwd',
  data() {
    return {
      phone: '',
      verificationCode: '',
      password: '',
      confirmPassword: '',
      showPassword: false,
      showConfirmPassword: false,
      countdown: 0
    }
  },
  onShow() {
    this.phone = uni.getStorageSync('customerInfo').cusPhone
  },
  methods: {
    goBack() {
      setTimeout(() => {
        uni.navigateBack()
      }, 100)
    },
    // 获取验证码
    getVerificationCode() {
      if (!this.phone) {
        uni.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
        return
      }

      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        })
        return
      }
      if (this.countdown > 0) {
        uni.showToast({
          title: `${this.countdown}s后可重新发送`,
          icon: 'none'
        })
        return
      }

      // 开始倒计时
      this.countdown = 60
      const timer = setInterval(() => {
        this.countdown--
        if (this.countdown <= 0) {
          clearInterval(timer)
        }
      }, 1000)

      let params = {
        cusPhone: this.phone,
        scene: 'UPDATE_PASSWD'
      }
      this.$http.post('/applet/customer/sendSms', params).then(res => {
        uni.showToast({
          title: '验证码已发送',
          icon: 'success'
        })
      })
    },

    // 切换密码显示
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword
    },

    // 切换确认密码显示
    toggleConfirmPasswordVisibility() {
      this.showConfirmPassword = !this.showConfirmPassword
    },

    // 提交表单
    handleSubmit() {

      if (!this.verificationCode) {
        uni.showToast({
          title: '请输入验证码',
          icon: 'none'
        })
        return
      }
      if (this.verificationCode.length !== 6) {
        uni.showToast({
          title: '请输入正确的6位验证码',
          icon: 'none'
        })
        return
      }

      if (!this.password) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return
      }
      if (this.password.length < 6 || this.password.length > 16) {
        uni.showToast({
          title: '密码长度不能少于6位，不能大于16位',
          icon: 'none'
        })
        return
      }
      if (this.password.includes(' ')) {
        uni.showToast({
          title: '密码不能包含空格',
          icon: 'none'
        })
        return
      }
      if (!/^[a-zA-Z0-9]+$/.test(this.password)) {
        uni.showToast({
          title: '密码只能包含数字或字母',
          icon: 'none'
        })
        return
      }

      if (!this.confirmPassword) {
        uni.showToast({
          title: '请再次输入密码',
          icon: 'none'
        })
        return
      }

      if (this.password !== this.confirmPassword) {
        uni.showToast({
          title: '两次输入的密码不一致',
          icon: 'none'
        })
        return
      }

      this.$http.post('/applet/customer/updatePassword', {
        cusId: uni.getStorageSync('customerInfo').id,
        validCode: this.verificationCode,
        newPassword: this.password
      }).then(res => {
        uni.showToast({
          title: '密码修改成功',
          icon: 'success'
        })
        // 返回上一页
        setTimeout(() => {
          uni.navigateBack()
        }, 500)
      })


    }
  }
}
</script>

<style scoped lang="scss">
@import '@/uni.scss';

.change-pwd-container {
  min-height: 100vh;
  background-color: #F5F5F6;
  padding: 20rpx 26rpx;
}

.form-container {
  background-color: #fff;
  border-radius: 8px;
  padding: 20rpx 26rpx;
}

.input-group {
  position: relative;
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
}

.input-field {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  border: 1px solid #EEEEEE;
  border-radius: 16rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  background-color: #fff;
  box-sizing: border-box;
}

.verification-input {
  width: calc(100% - 200rpx);
  margin-right: 10rpx;
}

.verification-btn {
  width: 200rpx;
  height: 60rpx;
  line-height: 60rpx;
  background-color: #393837;
  color: $uni-text-color-orange;
  border: none;
  font-size: 28rpx;
}

.verification-btn:disabled {
  background-color: #ccc;
  color: #999;
}

.password-input {
  padding-right: 50rpx;
  width: calc(100% - 60rpx);
}

.eye-icon {
  position: absolute;
  right: 0rpx;
  // top: 50%;
  // transform: translateY(-50%);
  // cursor: pointer;
}

.eye-text {
  font-size: 18rpx;
}

.confirm-btn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  background-color: #393837;
  color: #fff;
  border: none;
  border-radius: 16rpx;
  font-size: 28rpx;
  margin-top: 30rpx;
}

.confirm-btn:active {
  background-color: #393837;
}

/* 去除输入框聚焦时的默认边框/高亮 */
.input-field:focus {
  outline: none;
  box-shadow: none;
  -webkit-appearance: none;
  appearance: none;
}

/* H5 点击高亮取消 */
input {
  -webkit-tap-highlight-color: transparent;
}
</style>
